﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using EGym.BL;
using EGym.BL.ControleSessao;
using EGym.BL.Extensions;
using EGym.Data;

namespace EGym.Controllers
{
    public static class SessionManager
    {
        #region Constantes
        private const string GUID_COOKIE_KEY = "SessionGuid";
        #endregion

        #region Métodos

        private static string ProcessGuid()
        {
            Debug.WriteLine("ProcessGuid");
            string guid = string.Empty;

            if (!HttpContext.Current.Request.Cookies.AllKeys.Contains(GUID_COOKIE_KEY))
            {
                guid = GetAndSaveGuidInCookie();
            }
            else
            {
                guid = new SessaoGestor(new DBDataContext()).RevisaTicketSessao(CachedGuid).GUID;
                if (CachedGuid != guid)
                    SlideCookie(guid,1);
            }
                
            return guid;
        }

        private static void SlideCookie(string guid, int horas)
        {
            Debug.WriteLine("SlideCookie");
            HttpCookie sessionCookie = new HttpCookie(GUID_COOKIE_KEY, guid);
            sessionCookie.Expires = DateTime.Now.AddHours(horas);
            HttpContext.Current.Response.SetCookie(sessionCookie);
        }

        public static void Logoff()
        {
            Debug.WriteLine("Logoff");
            SessaoGestor sessaoGestor = new SessaoGestor(new DBDataContext());
            sessaoGestor.Logoff(sessaoGestor.RevisaTicketSessao(ProcessGuid()));
            if (HttpContext.Current.Response.Cookies.AllKeys.Contains(GUID_COOKIE_KEY))
                HttpContext.Current.Response.Cookies.Remove(GUID_COOKIE_KEY);

        }

        public static bool EstaAutenticado()
        {
            Debug.WriteLine("EstaAutenticado");
            SessaoGestor sessaoGestor = new SessaoGestor(new DBDataContext());
            string guid = ProcessGuid();
            bool retorno = sessaoGestor.EstaAutenticado(guid);
            return retorno;
        }
        
        public static Usuario UsuarioLogado
        {
            get
            {
                Debug.WriteLine("UsuarioLogado");
                SessaoGestor sessaoGestor = new SessaoGestor(new DBDataContext());
                Sessao sessao = sessaoGestor.RevisaTicketSessao(ProcessGuid());

                return sessao.SessaoUsuarios.FirstOrDefault().Usuario;
            }
        }

        public static string UsuarioLogadoTexto
        {
            get
            {
                string retorno = string.Empty;
                Usuario usuario = UsuarioLogado;
                if (usuario!= null)
                    retorno = string.Format("{0}@{1}", usuario.UserName, usuario.Afiliado.Nome.RemoveEspacos());
                return retorno;
            }
        }

        private static string GetAndSaveGuidInCookie()
        {
            Debug.WriteLine("GetAndSaveGuidInCookie");
            string guid;
            guid = new SessaoGestor(new DBDataContext()).CriaNovaSessao().GUID;
            SlideCookie(guid,1);
            return guid;
        }

        #endregion

        #region Properties
        public static Sessao Sessao
        {
            get
            {
                return new SessaoGestor(new DBDataContext()).RevisaTicketSessao(ProcessGuid());
            }
        }
        
        public static Usuario Usuario
        {
            get
            {
                SessaoUsuario sessaoUsuario = Sessao.SessaoUsuarios.FirstOrDefault();
                return sessaoUsuario != null ? sessaoUsuario.Usuario : null;
            }
        }

        public static string CachedGuid
        {
            get
            {
                string retorno = string.Empty;
                if (HttpContext.Current.Request.Cookies.AllKeys.Contains(GUID_COOKIE_KEY))
                    retorno = HttpContext.Current.Request.Cookies[GUID_COOKIE_KEY].Value;
                else
                {
                    retorno = GetAndSaveGuidInCookie();
                }
                return retorno;
            }
        }
        #endregion
    }
}
